package gui.cen.read;

import core.utils.Utils;
import gui.Segl2;
import gui.cen.Center;
import gui.cen.south.HeatMap;
import gui.processor.Processor;
import gui.processor.Worker;
import gui.utils.Borders;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.AdjustmentEvent;
import java.awt.event.AdjustmentListener;
import javax.swing.JScrollPane;
import gui.utils.Para;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JLayeredPane;
import javax.swing.JPanel;

/*Author: John Archer, Contact: john.archer.jpa@gmail.com*/
public class ReadDisp extends JPanel implements ActionListener{
    private JScrollPane scr;
    private int scrollYPos;
    private ReadPane rp;
    private JPanel save, p;
    private TreeDar tDar;
    private JLayeredPane lp;
    private Segl2 segl2;
    private JButton sr;
    private CurrFilter cf;
    private Center center;

    public ReadDisp(Center center, Segl2 segl2){
        this.setLayout(new BorderLayout());
        this.center = center;
        this.segl2 = segl2;
        lp = new JLayeredPane();

        rp = new ReadPane(segl2);
        rp.setBounds(0, 0, segl2.getWidth()-4, segl2.getHeight() - HeatMap.HEIGHT_OF_HEATMAP - 42);
        lp.add(rp, new Integer(1));

        tDar = new TreeDar();
        tDar.setBounds((segl2.getWidth() - (Center.WIDGE_WIDTH + Center.WIDGE_SIDE)), Center.WIDGE_TOP, Center.WIDGE_WIDTH, Center.WIDGE_WIDTH);
        lp.add(tDar, new Integer(2));

        cf = new CurrFilter();
        cf.setFilterLab();
        cf.setBounds((segl2.getWidth() - (Center.WIDGE_WIDTH + Center.WIDGE_SIDE)), (segl2.getHeight() - (HeatMap.HEIGHT_OF_HEATMAP + 130)), Center.WIDGE_WIDTH, CurrFilter.HEIGHT_OF_FILTER);
        lp.add(cf, new Integer(3));

        save = Utils.getButton(sr = new JButton("Save Mapped Reads"), 180, 25, new Color(102, 102, 102), new Color(178, 178, 178), this, "Save Reads at Current Filter Level");
        save.setOpaque(false);
        save.setBounds(30, Center.WIDGE_TOP, Center.WIDGE_WIDTH, 40);
        lp.add(save, new Integer(3));

        this.add(lp, BorderLayout.CENTER);

        p = new JPanel();
        p.setPreferredSize(new Dimension(1, Borders.SEQCHARHEIGHT * Para.RTD.size()));
        scr = new JScrollPane(p);
        scr.setBorder(BorderFactory.createEmptyBorder(0,0,0,0));
        scr.getViewport().setBackground(Color.white);
        scr.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
        scr.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        AdjustmentListener titleScrollAdjustmentListener = new AdjustmentListener() {
        public void adjustmentValueChanged(AdjustmentEvent adjustmentEvent){
                scrollYPos = scr.getVerticalScrollBar().getModel().getValue();
                int readInDisplayListToStartDisplay = scrollYPos / Borders.SEQCHARHEIGHT;
                int noOfReadsToViewInImage = (int)(getSize().height/Borders.SEQCHARHEIGHT) + 1;
                int readInDisplayListToEndDisplay = readInDisplayListToStartDisplay + noOfReadsToViewInImage;
                Para.START_VERTICAL_VIEW = readInDisplayListToStartDisplay;
                Para.END_VERTICAL_VIEW = readInDisplayListToEndDisplay;
                updateReads();
            }
        };
        scr.getVerticalScrollBar().addAdjustmentListener(titleScrollAdjustmentListener);
        this.add(scr, BorderLayout.EAST);

        this.addComponentListener(new ComponentAdapter(){
            public void componentResized(ComponentEvent e){updateReads();}
        });
    }

    public void setFilterLVL_label(){cf.setFilterLab();}
    public void updateTreedar(){tDar.updateTreedar();}

    public void updateReads(){
        rp.updateReads();
        lp.setPreferredSize(new Dimension(segl2.getWidth()-4, segl2.getHeight() - HeatMap.HEIGHT_OF_HEATMAP - 42));
        tDar.setBounds((segl2.getWidth() - (Center.WIDGE_WIDTH + Center.WIDGE_SIDE)), Center.WIDGE_TOP, Center.WIDGE_WIDTH, Center.WIDGE_WIDTH);
        rp.setBounds(0, 0, segl2.getWidth()-4, segl2.getHeight() - HeatMap.HEIGHT_OF_HEATMAP - 42);
        setFilterLVL_label();
        cf.setBounds((segl2.getWidth() - (Center.WIDGE_WIDTH + Center.WIDGE_SIDE)), (segl2.getHeight() - (HeatMap.HEIGHT_OF_HEATMAP + 130)), Center.WIDGE_WIDTH, CurrFilter.HEIGHT_OF_FILTER);
        p.setPreferredSize(new Dimension(1, Borders.SEQCHARHEIGHT * Para.RTD.size()));
        p.revalidate();
        save.setBounds(30, Center.WIDGE_TOP, Center.WIDGE_WIDTH, 40);
    }

    @Override
    public void actionPerformed(ActionEvent evt) {
        Object source = evt.getSource();
        if(source == sr){
            Processor p = new Processor(center);
            p.setTask(Para.TASK_RSAVE);
            Worker sl = new Worker(p);
            sl.execute();
        }
    }
}